﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using S4Store.Persistent;
using S4Store.Domain;
using LX.Core.Query;
using S4Store.Service.Valid.StoreValid;

namespace S4Store.Service
{
    public class CarService : BaseService
    {

        public S4Store.Service.Valid.CarValid.PinpaiAdd ValidPinpaiAdd  { get; set; }
        public S4Store.Service.Valid.CarValid.PinpaiModify ValidPinpaiModify { get; set; }

        public S4Store.Service.Valid.CarValid.CarBrandAdd ValidCarBrandAdd { get; set; }
        public S4Store.Service.Valid.CarValid.CarBrandModify ValidCarBrandModify { get; set; }
        public S4Store.Service.Valid.CarValid.CarBrandDelete ValidCarBrandDelete { get; set; }

        public S4Store.Service.Valid.CarValid.CarModelAdd ValidCarModelAdd { get; set; }
        public S4Store.Service.Valid.CarValid.CarModelModify ValidCarModelModify { get; set; }
        public S4Store.Service.Valid.CarValid.CarModelDelete ValidCarModelDelete { get; set; }

        public IQueryable<PinPai> GetPinPaiQuery()
        {
            return this.GetQuery<PinPai>();
        }

        public IQueryable<CarBrand> GetCarBrandQuery()
        {
            return this.GetQuery<CarBrand>();
        }
        public IQueryable<CarModel> GetCarModelQuery()
        {
            return this.GetQuery<CarModel>();
        }

        public IQueryable<PinPai> GetEnablePinPai()
        {
            return GetPinPaiQuery().Where(t => !t.IsClose);
        }

        public IQueryable<CarBrand> GetEnableCarBrand()
        {
            return GetCarBrandQuery().Where(t => !t.IsClose);
        }
        public IQueryable<CarBrand> GetEnableCarBrand(int? pinpaiid)
        {
            return GetCarBrandQuery().Where(t => !t.IsClose && t.PinPaiId != null && t.PinPaiId == pinpaiid);
        }
        public void AddPinpai(PinPai model)
        {
            this.Valid(model, ValidPinpaiAdd);
            this.GetSet<PinPai>().Add(model);
        }

        public void AddCarBrand(CarBrand model)
        {
            this.Valid(model, ValidCarBrandAdd);
            model.Name = model.Name.Trim();
            this.GetSet<CarBrand>().Add(model);
        }

        public void AddCarModel(CarModel model)
        {
            this.Valid(model, ValidCarModelAdd);
            model.Name = model.Name.Trim();
            this.GetSet<CarModel>().Add(model);
        }


        public void ModifyPinpai(PinPai model)
        {
            PinPai oldmodel = GetPinPaiQuery().First(t => t.Id == model.Id);
            oldmodel.Name = model.Name;
            oldmodel.IsClose = model.IsClose;
            this.Valid(oldmodel, ValidPinpaiModify);
        }

        public void ModifyCarBrand(CarBrand model)
        {
            CarBrand oldmodel = GetCarBrandQuery().First(t => t.Id == model.Id);
            oldmodel.Name = model.Name.Trim();
            oldmodel.IsClose = model.IsClose;
            oldmodel.PinPaiId = model.PinPaiId;
            this.Valid(oldmodel, ValidCarBrandModify);
        }

        public void ModifyCarModel(CarModel model)
        {
            CarModel oldmodel = GetCarModelQuery().First(t => t.Id == model.Id);
            oldmodel.BrandId = model.BrandId;
            oldmodel.Name = model.Name.Trim();
            oldmodel.IsClose = model.IsClose;
            this.Valid(oldmodel, ValidCarModelModify);
        }

        public void DeleteCarBrand(int id)
        {
            CarBrand model = GetCarBrandQuery().First(t => t.Id == id);
            if (model == null) return;
            this.Valid(model, ValidCarBrandDelete);
            this.GetSet<CarBrand>().Remove(model);
        }

        public void DeleteCarModel(int id)
        {
            CarModel model = GetCarModelQuery().First(t => t.Id == id);
            if (model == null) return;
            this.Valid(model, ValidCarModelDelete);
            this.GetSet<CarModel>().Remove(model);
        }

    }
}
